package serializer

import (
	"fmt"
	"singo/cache"
	"singo/model"
)

// User 用户序列化器
type User struct {
	UID          string      `json:"id"`
	UserName     string      `json:"username"`
	Nickname     string      `json:"nickname"`
	UserType     int         `json:"user_type"`
	Gender       int         `json:"gender"`
	Intro        string      `json:"intro"`
	Mobile       string      `json:"mobile"`
	Email        string      `json:"email"`
	Status       string      `json:"status"`
	Avatar       string      `json:"avatar"`
	Job          string      `json:"job"`
	Rank         int         `json:"rank"`
	Level        int         `json:"level"`
	Organization interface{} `json:"organization"`
}

// UserSimple 简单版用户序列化器
type UserSimple struct {
	UID      string `json:"id"`
	Nickname string `json:"nickname"`
}

// UserWithPerformance 用户序列化器
type UserWithPerformance struct {
	UID          string  `json:"id"`
	Nickname     string  `json:"nickname"`
	Organization string  `json:"organization"`
	Score        float64 `json:"score"`
}

// BuildUserSimple 简单版序列化用户
func BuildUserSimple(userID string) UserSimple {
	if userID != "" {
		// 用 Redis
		user, err := cache.RedisClient.HMGet(userID, "nickname").Result()
		if err != nil {
			fmt.Println("User List Get RedisHash Redis Error", err)
		}
		//用 数据库
		// user := model.User{}
		// model.DB.First(&user, "uid = ?", userID)

		return UserSimple{
			UID:      userID,
			Nickname: user[0].(string),
			// Nickname: user.Nickname,
		}
	} else {
		return UserSimple{}
	}
}

// BuildUserWithPerformance 简单版序列化用户
func BuildUserWithPerformance(user model.UserWithPerformance) UserWithPerformance {
	return UserWithPerformance{
		UID:          user.UID,
		Nickname:     user.Nickname,
		Organization: BuildOrganizationSimple(user.OrganizationUID).Name,
		Score:        user.Score,
	}
}

// BuildUser 序列化用户
func BuildUser(user model.User) User {
	if user.OrganizationUID == "" {
		return User{
			UID:      user.UID,
			UserName: user.UserName,
			Nickname: user.Nickname,
			UserType: user.UserType,
			Gender:   user.Gender,
			Intro:    user.Intro,
			Mobile:   user.Mobile,
			Email:    user.Email,
			Status:   user.Status,
			Avatar:   user.Avatar,
			Job:      user.Job,
			Rank:     user.Rank,
			Level:    user.Level,
		}
	}
	return User{
		UID:          user.UID,
		UserName:     user.UserName,
		Nickname:     user.Nickname,
		UserType:     user.UserType,
		Gender:       user.Gender,
		Intro:        user.Intro,
		Mobile:       user.Mobile,
		Email:        user.Email,
		Status:       user.Status,
		Avatar:       user.Avatar,
		Job:          user.Job,
		Rank:         user.Rank,
		Level:        user.Level,
		Organization: BuildOrganizationSimple(user.OrganizationUID),
	}
}

// BuildUserAvatar 序列化用户头像
func BuildUserAvatar(user model.User) User {
	return User{
		Avatar: user.Avatar,
	}
}

// BuildUserResponse 序列化用户响应
func BuildUserResponse(user model.User) Response {
	return Response{
		Data: BuildUser(user),
	}
}

// BuildUsers 序列化列表
func BuildUsers(items []model.User) (users []User) {
	for _, item := range items {
		user := BuildUser(item)
		users = append(users, user)
	}
	return users
}

// BuildUsersWithPerformance 序列化列表
func BuildUsersWithPerformance(items []model.UserWithPerformance) (users []UserWithPerformance) {
	for _, item := range items {
		user := BuildUserWithPerformance(item)
		users = append(users, user)
	}
	return users
}
